using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Serialization;

public enum GolemStates { Follow, Attack, DIE };
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(CharacterStats))]
public class GolemClone : MonoBehaviour
{
    private NavMeshAgent agent;
    public GolemStates golemStates;
    protected GameObject attackTarget;
    private Animator anim;
    private float speed;
    protected CharacterStats characterStats;
    private float lastAttackTime;


    bool isWalk;
    bool isChase;
    bool isFollow;
    bool isDeath;

    [Header("Basic setting")]
    public float sightRadius;
    


    private LootItem _lootItem;
    // Start is called before the first frame update
    private void Awake()
    {
        agent = GetComponent<NavMeshAgent>();
        anim = GetComponent<Animator>();
        characterStats = GetComponent<CharacterStats>();
        speed = agent.speed;
        StartCoroutine("Die");
    }
 
    public virtual void Update()
    {
        SwitchStates();
        SwitchAnimation();
        lastAttackTime -= Time.deltaTime;
        
    }
    void SwitchAnimation()
    {
        anim.SetBool("Walk", isWalk);
        anim.SetBool("Chase", isChase);
        anim.SetBool("Follow", isFollow);
        anim.SetBool("Death", isDeath);
    }

    
    public void SwitchStates()
    {
        if (isDeath)
        {
            golemStates = GolemStates.DIE;
        }
        else if (FoundEnemy())
        {
            golemStates = GolemStates.Attack;
        }
        else if (!FoundEnemy())
        {
            golemStates = GolemStates.Follow;
        }
        switch (golemStates)
        {
            case GolemStates.Follow :
                isWalk = true;
                isChase = false;
                agent.isStopped = false;
                transform.LookAt(GameManager.Instance.playerStats.transform.position);
                agent.destination = GameManager.Instance.playerStats.transform.position;

                if (Vector3.SqrMagnitude(transform.position-GameManager.Instance.playerStats.transform.position)<=characterStats.attackData.attackRange)
                { 
                    isWalk = false;
                }
                break;
            
            case GolemStates.Attack:
                isChase = true;
                isFollow = true;
                transform.LookAt(attackTarget.transform.position);
                agent.isStopped = false;
                agent.speed = speed * 1.5f;
                agent.destination = attackTarget.transform.position;
                if (Vector3.Distance(attackTarget.transform.position, transform.position) <= characterStats.attackData.attackRange)
                {
                    isFollow = false;
                    agent.isStopped = true;
                    if (lastAttackTime < 0)
                    {
                        lastAttackTime = characterStats.attackData.coolDown;
                        //�����ж�
                        Attack();
                    }
                }
                break;
            case GolemStates.DIE:
                agent.radius = 0;
                isDeath = true;
                Destroy(gameObject, 2.0f);
                break;
        }

        
            
    }
    void Attack()
    {
        transform.LookAt(attackTarget.transform);
        anim.SetTrigger("Attack");
        Debug.Log("dale");
    }


    bool FoundEnemy()
    {
        var colliders = Physics.OverlapSphere(transform.position, sightRadius);
        foreach(var target in colliders)
        {
            if (target.CompareTag("Enemy"))
            {
                attackTarget = target.gameObject;
                return true;
            }
        }
        return false;
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.blue;
        Gizmos.DrawWireSphere(transform.position, sightRadius);
    }

    void Hit()
    {
        if (attackTarget!= null&&transform.IsFacingTarget(attackTarget.transform)){
            var targetStats = attackTarget.GetComponent<CharacterStats>();
        targetStats.TakeDamage(characterStats, targetStats);
        }
    }

    IEnumerator Die()
    {
        yield return new WaitForSeconds(60f);
        isDeath = true;
    }
    
    
}
